package psrc.bd.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import psrc.bd.util.ConnectionFactory;
import psrc.logic.util.beans.ClienteBean;

public class ConsultaClientesRedesDAO {

	protected Connection connection;

	public ConsultaClientesRedesDAO(Connection conn) {
		if (conn != null) {
			this.connection = conn;
		} else {
			this.connection = new ConnectionFactory().getConnection();
		}
	}

	public List<ClienteBean> consultaPedido(int codigoRede)
			throws SQLException {

		List<ClienteBean> clientes = new ArrayList<ClienteBean>();

		try {
			String sql = "SELECT * FROM Cliente c, Representante rep, Rede rede WHERE c.fk_rede=? AND rede.id_rede = c.fk_rede AND rep.id_representante = c.fk_representante;";

			PreparedStatement stmt = connection.prepareStatement(sql);
			stmt.setInt(1, codigoRede);

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {
				ClienteBean bean = new ClienteBean();
				bean.setRazaoSocial(rs.getString("razao_social"));
				bean.setNomeFantasia(rs.getString("nome_fantasia"));
				bean.setLogradouro(rs.getString("logradouro"));
				bean.setNumero(rs.getString("numero"));
				bean.setComplemento(rs.getString("complemento"));
				bean.setBairro(rs.getString("bairro"));
				bean.setCidade(rs.getString("cidade"));
				bean.setEstado(rs.getString("estado"));
				bean.setCep(rs.getString("cep"));
				bean.setCnpj(rs.getString("cnpj"));
				bean.setInscricaoEstadual(rs.getString("inscricao_estadual"));
				bean.setTelefone1(rs.getString("telefone_1"));
				bean.setTelefone2(rs.getString("telefone_2"));
				bean.setEmail(rs.getString("email"));
				bean.setComprador(rs.getString("comprador"));
				bean.setFkRepresentante(rs.getInt("fk_representante"));
				bean.setFkRede(rs.getInt("fk_rede"));
				bean.setIdCliente(rs.getString("id_cliente"));

				clientes.add(bean);
			}
			rs.close();
			stmt.close();
			return (List<ClienteBean>) clientes;
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}
}
